//
//  HMStatusPictureView.swift
//  weibo
//
//  Created by LCH on 16/3/10.
//  Copyright © 2016年 lch. All rights reserved.
//

import UIKit
import SDWebImage
// 图片之间的间距
private let itemMargin: CGFloat = 5
// 每一个条目的大小
private let itemWH = CGFloat(Int(SCREENW - 2 * Margin - 2 * itemMargin)/3)

class HMStatusPictureView: UICollectionView {
    
    
    private let HMStatusPictureViewCellId = "HMStatusPictureViewCellId"
    
    var picUrls:[HMStatusPictureInfo]? {
    
        didSet {
        
         label.text = "\(picUrls?.count ?? 0)"
            
         self.snp_updateConstraints { (make) -> Void in
            
            make.size.equalTo(self.sizeWithCount(picUrls?.count ?? 0))
            }
         self.reloadData()
        }
    
        
    
    
    }
    

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: layout)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func sizeWithCount(count: Int) -> CGSize {
    
//        let itemMargin:CGFloat = 5
        
//        let itemWh = CGFloat(Int(SCREENW - 2 * Margin - 2 * itemMargin)/3)
        
        
        if count == 1 {
        
        if let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(picUrls!.first?.thumbnail_pic){
        
        var size = image.size
            
            if size.width < 80 {
            
             size.height = 80/size.width * size.height
             size.width = 80
            
            }
            let layout = self.collectionViewLayout as? UICollectionViewFlowLayout
            layout?.itemSize = size
            return size
        }
        }
        
        let layout = self.collectionViewLayout as? UICollectionViewFlowLayout
        
        layout?.itemSize = CGSize(width: itemWH, height: itemWH)
        
        let col = count == 4 ? 2 : (count > 3 ? 3: count)
        let row = (count - 1)/3 + 1
        
        let width = CGFloat(col)*itemWH + CGFloat(col - 1) * itemMargin
        let height = CGFloat(row) * itemWH + CGFloat(row) * itemMargin
        
        return CGSize(width: width, height: height)
    
    
    
    }
    
      private func setupUI() {
        
        self.dataSource = self
        
        registerClass(HMStatusPictureViewCell.self, forCellWithReuseIdentifier: HMStatusPictureViewCellId)
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSizeMake(itemWH, itemWH)
        
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        
        backgroundColor = randomColor()
        
        addSubview(label)
        
       label.snp_makeConstraints { (make) -> Void in
        
        make.center.equalTo(self)
        }
    }
    
    
    
    private lazy var label:UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 35)
}

// TODO: ???

extension HMStatusPictureView:UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return picUrls?.count ?? 0
    }

    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HMStatusPictureViewCellId, forIndexPath: indexPath) as! HMStatusPictureViewCell
        
//        cell.backgroundColor = randomColor()
        
        cell.pictureInfo = picUrls![indexPath.item]
        
        return cell
    }


}

class HMStatusPictureViewCell:UICollectionViewCell {
    
    var pictureInfo:HMStatusPictureInfo? {
    
        didSet {
        
            guard let urlString = pictureInfo?.thumbnail_pic else {
            
              return
            
            }
         imageView.sd_setImageWithURL(NSURL(string: urlString), placeholderImage: UIImage(named: "timeline_image_placeholder"))
        
        }
    
    
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
    contentView.addSubview(imageView)
     
    imageView.snp_makeConstraints { (make) -> Void in
        
        make.edges.equalTo(contentView)
        }
    }
    
    
    private lazy var imageView: UIImageView = {
    
     let imageView = UIImageView()
    
    imageView.contentMode = .ScaleAspectFill
        
    imageView.clipsToBounds = true
        
        return imageView
    
    }()
    
}

